package com.happystreet.utils
{
	import com.happystreet.config.GoodsConfig;
	
	import flash.display.DisplayObject;

	/**
	 * 工具类
	 * @author Liseen
	 * 
	 */	
	public class CustomUtils
	{
		public static var cumulatedTimeObj:Object;
		
		public function CustomUtils()
		{
		}
		
		/**
		 *根据XML返回Boolean 
		 * @param spun
		 * @param value
		 * @return 
		 * 
		 */		
		public static function booleanFromXmlString(spun:String, value:Boolean):Boolean
		{
			if (spun == null)
			{
				return value;
			}
			switch (spun.toLowerCase())
			{
				case "true":
					return true;
				case "false":
					return false;
				case "0":
					return false;
				default:
					return value;
			}
		}
		
		/**
		 *随即打乱数组 
		 * @param list
		 * @return 
		 * 
		 */		
		public static function randomizeArray(list:Array):Array
		{
			var randomIndex:int;
			var tempList:Array = new Array();
			var cloneList:Array = list.slice();
			while (cloneList.length > 0)
			{
				randomIndex = Math.floor(Math.random() * cloneList.length);
				tempList.push(cloneList.splice(randomIndex, 1)[0]);
			}
			return tempList;
		}
		/**
		 *存储物品时间 
		 * @param key	键
		 * @param time	时间
		 * 
		 */		
		public static function storeTime(key:String, time:uint):void
		{
			if (cumulatedTimeObj === null)
			{
				cumulatedTimeObj = new Object();
			}
			if (!cumulatedTimeObj.hasOwnProperty(key))
			{
				cumulatedTimeObj[key] = 0;
			}
			cumulatedTimeObj[key] += time;
		}
		
		public static function formatBigNumbers(num:int, _arg2:uint=0):String
		{
			var _local4:String;
			var _local6:int;
			var _local3:String = (_arg2!=0) ? _arg2.toString() : num.toString();
			var _local5:Array = new Array();
			_local6 = 0;
			while ((_local6 + 3) < _local3.length)
			{
				_local5.unshift(_local3.substring((_local3.length - (_local6 + 3)), (_local3.length - _local6)));
				_local6 = (_local6 + 3);
			}
			var _local7:int = (_local3.length - _local6);
			if (_local7 > 0)
			{
				_local5.unshift(_local3.substr(0, _local7));
			}
			_local4 = _local5.join(",");
			return _local4;
		}
		
		public static function replaceGameObjectReferencesIn(_arg1:String):String
		{
			var _local5:String;
			var _local6:String;
			var _local7:String;
			var _local8:String;
			var _local9:Object;
			var _local10:Object;
			var _local2:RegExp = / \{ ([A-Za-z]*) _ (\d*) (_ ([A-Za-z]*) )? \} /gx;
			if (_arg1 == null)
			{
				return (null);
			}
			var _local3:String = new String(_arg1);
			var _local4:Object = _local2.exec(_local3);
			while (_local4 != null)
			{
				_local5 = _local4[0];
				_local6 = _local4[1];
				_local7 = _local4[2];
				_local8 = _local4[4];
				if (_local6 == "good")
				{
					_local10 = GoodsConfig.goods;
					_local8 = (((_local8)==null) ? "name" : _local8);
					_local9 = _local10[_local7];
					_local3 = _local3.replace(_local5, _local9[_local8]);
				}
				_local4 = _local2.exec(_local3);
			}
			return _local3;
		}
		
		public static function replaceText(_arg1:String, _arg2:String, _arg3:String):String
		{
			var _local4:int = _arg2.length;
			var _local5:String = _arg1.substr(0, _arg1.indexOf(_arg2));
			var _local6:String = _arg1.substr((_local5.length + _local4));
			var _local7:String = ((_local5 + _arg3) + _local6);
			return _local7;
		}
		
		public static function getMaxInscribeScale(_arg1:DisplayObject, _arg2:*):Number
		{
			var _local3:Number;
			var _local4:Number;
			if ((_arg2 is DisplayObject))
			{
				_local3 = _arg2.width;
				_local4 = _arg2.height;
			}
			else
			{
				if ((_arg2 is Array))
				{
					_local3 = _arg2[0];
					_local4 = ((_arg2[1]) ? _arg2[1] : _arg2[0]);
				}
				else
				{
					if ((((((_arg2 is Number)) || ((_arg2 is int)))) || ((_arg2 is uint))))
					{
						_local4 = _arg2;
						_local3 = _local4;
					}
				}
			}
			var _local5:Number = (_arg1.width / _arg1.height);
			var _local6:Number = (_local3 / _local4);
			if (_local5 >= _local6)
			{
				return ((_local3 / _arg1.width));
			}
			return ((_local4 / _arg1.height));
		}
	}
}